from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('demo_332_gridlayout')
        self.resize(500, 500)
        self.setup_ui()
        QMetaObject.connectSlotsByName(self)

    def setup_ui(self):
        self.gridlayout_test()

    def gridlayout_test(self):
        gridlayout = QGridLayout()

        self.setLayout(gridlayout)

        label1 = QLabel('标签1')
        label1.setObjectName('label1')

        label2 = QLabel('标签2')
        label2.setObjectName('label2')

        label3 = QLabel('标签3')
        label3.setObjectName('label3')

        label5 = QLabel('标签5')
        label5.setObjectName('label5')

        label6 = QLabel('标签6')
        label6.setObjectName('label6')

        label7 = QLabel('标签7')
        label7.setObjectName('label7')

        self.setStyleSheet("""
                          QLabel#label1{
                             background-color: cyan;
                         }
                         QLabel#label2{
                             background-color: yellow;
                         }
                         QLabel#label3{
                             background-color: red;
                         }
                         QLabel#label5{
                             background-color: pink;
                         }
                         QLabel#label6{
                             background-color: blue;
                         }
                         QLabel#label7{
                             background-color: cyan;
                         }
                     """)
        h_layout = QHBoxLayout()
        h_layout.addWidget(label5)
        h_layout.addWidget(label6)
        h_layout.addWidget(label7)


        gridlayout.addWidget(label1, 0, 0)
        gridlayout.addWidget(label2, 1, 1)
        gridlayout.addWidget(label3, 2, 2)

        gridlayout.addLayout(h_layout, 3, 1, 3, 3)
        #gridlayout.addLayout(h_layout, 3, 0, 1, 4)
        #gridlayout.addChildLayout(h_layout)
        gridlayout.setSpacing(10)
        print(gridlayout.rowCount())


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Window()
    win.show()

    sys.exit(app.exec_())
